package cn.wangchao.org.service.impl;

import cn.wangchao.basic.exception.BusinessException;
import cn.wangchao.basic.util.BaiduAuditUtils;
import cn.wangchao.org.domain.Employee;
import cn.wangchao.org.domain.Shop;
import cn.wangchao.org.domain.ShopAuditLog;
import cn.wangchao.org.mapper.EmployeeMapper;
import cn.wangchao.org.mapper.ShopAuditLogMapper;
import cn.wangchao.org.mapper.ShopMapper;
import cn.wangchao.org.service.IShopService;
import cn.wangchao.basic.service.impl.BaseServiceImpl;
import cn.wangchao.org.vo.ShopVo;
import org.apache.velocity.runtime.directive.Foreach;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wangchao
 * @since 2022-06-12
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;
    @Autowired
    private JavaMailSender javaMailSender;
    /**
     * 1.判断输入内容是否为空
     * //合法校验
     * 2.判断店铺不能被注册过
     * 3.判断人是否注册过
     *      注册过有没有绑定店铺,绑定就换个人,没有绑定就绑定
     * 4.保存数据
     *
     * @param shop
     */
    @Override
    public void settlement(Shop shop) {
        Employee admin = shop.getAdmin();
        if (StringUtils.isEmpty(admin)
                || StringUtils.isEmpty(shop.getName())
                || StringUtils.isEmpty(admin.getUsername())
        ){
            throw new BusinessException("内容不能为空,请重新输入");
        }
        if (!BaiduAuditUtils.TextCensor(shop.getName())){
            throw new BusinessException("你怕是在想屁吃???小伙子");
        };
        String log = "http://123.207.27.208"+shop.getLogo();
        if (!BaiduAuditUtils.ImgCensor(log)){
            throw new BusinessException("你怕是想进去吃轻松饭哦!!!");
        }
        Shop shopt = shopMapper.loadByName(shop.getName());
        //判断店铺是否存在,不为null说明店铺存在
        if (shopt != null){
            throw new BusinessException("店铺名已注册,请直接登录");
        }
        Employee empt = employeeMapper.loadByUsername(admin.getUsername());
        //判断用户是否存在,为null说明已经注册
        if (empt != null && empt.getShop_id() != null){
            //注册过用户并且绑定了店铺
            throw new BusinessException("用户已有在注册店铺,换个人来");
        }
        //注册过用户,没有绑定店铺,直接绑定
        if (empt == null){
            employeeMapper.save(admin);
            empt = admin;
        }//保存数据
        shop.setAdminId(empt.getId());
        shopMapper.save(shop);
        empt.setShop_id(shop.getId());
        employeeMapper.update(empt);
    }

    /**
     * 审核通过
     * @param log
     */
    @Override
    public void pass(ShopAuditLog log) {
        //修改店铺状态为审核通过
        Shop shop = shopMapper.loadById(log.getShopId());
        shop.setState(1);
        shopMapper.update(shop);
        //记录日志
        log.setAuditId(334L);
        shopAuditLogMapper.save(log);
        //发送激活邮件
        SimpleMailMessage mail = new SimpleMailMessage();
        //设置发送人
        mail.setFrom("2495479629@qq.com");
        //邮件主题
        mail.setSubject("宠物乐园-审核结果告知");
        //邮件内容
        mail.setText("恭喜你审核通过,请尽快点击下方链接进行激活并登录,激活链接:http://localhost:8080/shop/activation/"+log.getShopId());
        //收件人
        mail.setTo("2495479629@qq.com");
        javaMailSender.send(mail);
    }

    /**
     * 审核驳回
     * @param log
     */
    @Override
    public void reject(ShopAuditLog log) {
        Employee employee = new Employee();
        //修改店铺状态为审核驳回
        Shop shop = shopMapper.loadById(log.getShopId());
        shop.setState(-1);
        shopMapper.update(shop);
        //记录日志
        log.setAuditId(334L);
        shopAuditLogMapper.save(log);
        //发送激活邮件
        SimpleMailMessage mail = new SimpleMailMessage();
        //设置发送人
        mail.setFrom("2495479629@qq.com");
        //邮件主题
        mail.setSubject("宠物乐园-审核结果告知");
        //邮件内容
        mail.setText("非常抱歉!你的店铺审核被驳回,请尽快点击下方链接进行修改,修改链接:http://localhost:8080/shop/activation/"+log.getShopId());
        //收件人
        mail.setTo("2495479629@qq.com");
        javaMailSender.send(mail);
    }

    @Override
    public void batchSave(List<Shop> shops) {
        shopMapper.batchSave(shops);
    }

    /**
     * 报表
     */
    @Override
    public Map<String,Object> queryEcharts() {
        List<String> x = new ArrayList<>();
        List<Integer> y = new ArrayList<>();
        List<ShopVo> shopVos = shopMapper.queryEcharts();
        for (ShopVo shopVo:shopVos){
            if (shopVo.getState()==1){
                x.add("审核通过");
            }
            if (shopVo.getState()==0){
                x.add("待审核");
            }
            if(shopVo.getState()==-1){
                x.add("驳回");
            }
            if(shopVo.getState()==2){
                x.add("已激活");
            }
            y.add(shopVo.getNumber());
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("x",x);
        map.put("y",y);
        return map;
    }

    @Override
    public void activation(Long id) {
        Shop shop = shopMapper.loadById(id);
        shop.setState(2);
        shopMapper.update(shop);
    }


}
